<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h2>深拷贝</h2>
</body>
<script>
    //1.对象深拷贝
    let obj = {
            name: 'alice',
            age: 20,
            un: undefined,
            fn: function() {},
            address: {
                ad1: '地址1',
                ad2: '地址2',
            }
        }
        //实现思路：层层判断该值是否是对象，如果继续递归，直到不是对象为止
    function cloneDeep(target) {

        //先创建一个存放深拷贝结果的变量
        let res = Array.isArray(target) ? [] : {}

        //遍历target  for in 

        for (let key in target) {

            res[key] = typeof target[key] === 'object' ? cloneDeep(target[key]) : target[key]

        }

        return res
    }

    let obj2 = cloneDeep(obj)


    console.log('obj:', obj)
    console.log('obj2:', obj2)
</script>

</html>